package com.hm.cms.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.hm.cms.auto.SystemEnvConfigProperties;
import com.hm.cms.service.ArticleService;
import com.hm.cms.service.TermTaxonomyService;
import com.hm.cms.service.TermTayRelationshipsService;
import com.hm.cms.service.TermsService;
import com.hm.cms.vo.HcmsHomePageVo;
import com.hm.cms.vo.HcmsPageBaseInfoVo;
import com.hm.cms.vo.HcmsTermTaxonomyVo;
import com.hm.cms.vo.HcmsTermsVo;
import com.hm.common.util.EncryptUtil.Base64;

/**
 * @author shishun.wang
 * @date 2017年10月28日 上午9:29:16
 * @version 1.0
 * @describe
 */
@Controller
@RequestMapping("article")
public class ArticleController {

	@Autowired
	private TermsService termsService;
	
	@Autowired
	private ArticleService articleService;

	@Autowired
	private TermTaxonomyService termTaxonomyService;

	@Autowired
	private SystemEnvConfigProperties systemEnvConfigProperties;
	
	@Autowired
	private TermTayRelationshipsService termTayRelationshipsService;

	/**
	 * @param termTayCode
	 *            标签code
	 * @param termCode
	 *            分类code
	 * @param articleId
	 *            文章id
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/{termTayCode}/{termCode}/{articleId}", method = RequestMethod.GET)
	public String dynaArticleDetail(@PathVariable("termTayCode") String termTayCode, @PathVariable("termCode") String termCode,
			@PathVariable("articleId") String articleId, Model model) {

		HcmsPageBaseInfoVo baseInfoVo = new HcmsPageBaseInfoVo();
		{
			baseInfoVo.setOpenLogin(systemEnvConfigProperties.isOpenLogin());
			baseInfoVo.setSiteFtInfo(new String(Base64.decode(systemEnvConfigProperties.getSiteFooterInfo().toCharArray())));
		}

		HcmsTermTaxonomyVo termTaxonomyVo = termTaxonomyService.getTermTaxonomysByCode(termTayCode);
		model.addAttribute("termTay", termTaxonomyVo);
		
		HcmsTermsVo termsVo = termsService.getHcmsTermsByCode(termCode);
		model.addAttribute("term", termsVo);
		
		model.addAttribute("baseInfo", baseInfoVo);
		model.addAttribute("article", articleService.getArticleInfo(articleId));
		
		HcmsHomePageVo homePageVo = new HcmsHomePageVo();
		{
			homePageVo.setWebPageNavs(termTaxonomyService.loadSysNavs());
		}
		model.addAttribute("homePageInfo", homePageVo);
		model.addAttribute("basePath", systemEnvConfigProperties.getStaticHtmlDomainName());
		model.addAttribute("recommendArticle", articleService.recommendArticle(termsVo.getId()));
		
		termTayRelationshipsService.statisticsViewQuantity(articleId);

		return "article/article_detail";
	}
}
